<!doctype html>
<html lang="en" class="fixed">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <title>渠道管理</title>
    <link rel="apple-touch-icon" sizes="120x120" href="favicon/apple-icon-120x120.png">
    <link rel="icon" type="image/png" sizes="192x192" href="favicon/android-icon-192x192.png">
    <link rel="icon" type="image/png" sizes="32x32" href="favicon/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="favicon/favicon-16x16.png">
    <link rel="stylesheet" type="text/css"
          href="<?php xcho(loadSource('/assets/plugins/bootstrap/css/bootstrap.min.css')) ?>">
    <link rel="stylesheet" href="<?php xcho(loadSource('/assets/plugins/animate.css/animate.css')) ?>">
    <link rel="stylesheet" href="<?php xcho(loadSource('/assets/plugins/font-awesome/css/font-awesome.css')) ?>">
    <link rel="stylesheet"
          href="<?php xcho(loadSource('/assets/plugins/data-table/media/css/dataTables.bootstrap.min.css')) ?>">
<!--    <link rel="stylesheet" href="--><?php //xcho(loadSource('/assets/plugins/ztree/css/zTreeStyle/zTreeStyle.css')) ?><!--">-->
    <link rel="stylesheet" href="<?php xcho(loadSource('/assets/plugins/ztree/css/metroStyle/metroStyle.css')) ?>" type="text/css">
    <link rel="stylesheet" href="<?php xcho(loadSource('/app/css/style.css')) ?>">
    <link rel="stylesheet" href="<?php xcho(loadSource('/app/css/app.css')) ?>">
    <link rel="stylesheet" href="<?php xcho(loadSource('/assets/plugins/layui/css/layui.css')) ?>" media="all">
    <link rel="stylesheet" href="<?php xcho(loadSource('/assets/css/new_main.css')) ?>">
    <style>
        .pagination{
            margin: 0;
        }
        .ztree li a{
            vertical-align: inherit;
        }
        #personList{
            padding: 14px;
        }
        #personList li{
            text-align: center;
            line-height: 28px;
        }
    </style>
</head>

<body><?php include_once TPL_PATH . 'public/nav.phtml'; ?>

<div class="wrap">
    <div class="page-body" style="padding: 0">
        <div class="content" style="margin: 0;background: #ececec">
            <div class="top_btn">
                <button class="btn btn-wide btn-primary"  id="addParent">添加</button>
            </div>
            <div class="row">
                <div class="col-sm-12">
                    <div class="panel">
                        <div class="panel-header">
                            <span>角色权限</span>
                        </div>
                        <div class="panel-content">
                            <!--                            <input type="text" class="form-control" id="placeholder" placeholder="请输入内容">-->
                            <ul class="ztree" id="treeDemo"></ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <a href="#" class="scroll-to-top"><i class="fa fa-angle-double-up"></i></a>
    </div>
</div>
<div id="modalbox"></div>
<div class="modal fade" id="info-modal-role" tabindex="-1" role="dialog" aria-labelledby="modal-info-label">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header state modal-info">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="modal-info-label"><i class="fa fa-info"></i></h4>
            </div>
            <div class="modal-body" style="min-height: 150px;">
                <ul class="ztree" id="role"></ul>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" id="sureRole" data-dismiss="modal">确定</button>
                <button type="button" class="btn btn-default" id="returnRole" data-dismiss="modal">返回</button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
    <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content">
            <ul id="personList">
            </ul>
        </div>
    </div>
</div>
<input type="hidden" name="list" value="<?php xcho($list) ?>"/>
<input type="hidden" name="add" value="<?php xcho($add) ?>"/>
<input type="hidden" name="delete" value="<?php xcho($delete) ?>"/>
<input type="hidden" name="save" value="<?php xcho($save) ?>"/>
<input type="hidden" name="action" value="<?php xcho($action) ?>"/>
<input type="hidden" name="model" value="<?php xcho($model) ?>"/>
<input type="hidden" name="user" value="<?php xcho($user) ?>"/>
<script src="<?php xcho(loadSource('/assets/plugins/jquery/jquery.min.js')) ?>"></script>
<script src="<?php xcho(loadSource('/assets/plugins/bootstrap/js/bootstrap.min.js')) ?>"></script>
<script src="<?php xcho(loadSource('/assets/plugins/data-table/media/js/jquery.dataTables.min.js')) ?>"></script>
<script src="<?php xcho(loadSource('/assets/plugins/data-table/media/js/dataTables.bootstrap.min.js')) ?>"></script>
<script src="<?php xcho(loadSource('/assets/plugins/layer-v3.0.3/layer/layer.js')) ?>"></script>
<script
    src="<?php xcho(loadSource('/assets/plugins/data-table/extensions/Responsive/js/dataTables.responsive.min.js')) ?>"></script>
<script
    src="<?php xcho(loadSource('/assets/plugins/data-table/extensions/Responsive/js/responsive.bootstrap.min.js')) ?>"></script>
<script src="<?php xcho(loadSource('/assets/plugins/ztree/js/jquery.ztree.core.js')) ?>"></script>
<script type="text/javascript" src="<?php xcho(loadSource('/assets/plugins/ztree/js/jquery.ztree.excheck.js')) ?>"></script>
<script type="text/javascript" src="<?php xcho(loadSource('/assets/plugins/ztree/js/jquery.ztree.exedit.js')) ?>"></script>
<script>
    //查看角色列表接口
    function jsList(role_id){
        var user = $("input[name='user']").val();
        $.ajax({
            url:user,
            type:'POST',
            data:{
                role_id:role_id
            },
            dataType:'json',
            success:function(data){
                $("#personList").html('');
                if(typeof(data['total'])){
                    $.each(data['data'],function (iKey,aData) {
                        $("#personList").append(
                            "<li>"+aData['full_name']+"</li>"
                        )
                    })
                }
            }
        })
    }jsList();
    $(function () {
        "use strict";
        var setting = {
            view: {
                selectedMulti: false,
                addHoverDom: addHoverDom,
                removeHoverDom:removeHoverDom
            },
            edit: {
                enable: false
            },
            data: {
                keep: {
                    parent:true,
                    leaf:true
                },
                simpleData: {
                    enable: true
                }
            },
            callback: {
                beforeDrag: beforeDrag,
                beforeRemove: beforeRemove,
                onClick:zTreeOnClick
            }
        };
        var zNodes = getListData();
        //console.log(zNodes);
        $.each(zNodes,function(i,item){
            item.isParent = true;
        })
        function zTreeOnClick(event,treeId,treeNode){
            // alert(treeNode.tId + "," + treeNode.name);
            jsList(treeNode.id)
        }
        var log, className = "dark";
        function beforeDrag(treeId, treeNodes) {
            return false;
        }

        function beforeRemove(treeId, treeNode) {
            className = (className === "dark" ? "":"dark");
            //console.log("[ beforeRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            zTree.selectNode(treeNode);
            return confirm("确认删除 " + treeNode.name + " 吗？");
        }


        var IDMark_Switch = "_switch",
            IDMark_Icon = "_ico",
            IDMark_Span = "_span",
            IDMark_Input = "_input",
            IDMark_Check = "_check",
            IDMark_Edit = "_edit",
            IDMark_Remove = "_remove",
            IDMark_Ul = "_ul",
            IDMark_A = "_a";
        //添加列表操作按钮
        function addHoverDom(treeId, treeNode) {
            if (treeNode.parentNode && treeNode.parentNode.id!=1) return;
            var aObj = $("#" + treeNode.tId + IDMark_A);
            if ($("#diyBtn1_"+treeNode.id).length>0) return;
            var editStr = "<button class='layui-btn layui-btn-mini' id='diyBtn1_" +treeNode.id+ "' title='"+treeNode.name+"' onfocus='this.blur();'><i class='layui-icon'>&#xe654;</i>添加角色</button><button class='layui-btn layui-btn-mini layui-btn-normal' id='diyBtn6_" +treeNode.id+ "' title='"+treeNode.name+"' onfocus='this.blur();'><i class='layui-icon'>&#xe642;</i>编辑角色</button><button class='layui-btn layui-btn-mini' id='diyBtn_" +treeNode.id+ "' title='"+treeNode.name+"' onfocus='this.blur();'><i class='layui-icon'>&#xe654;</i>复制角色</button><button class='layui-btn layui-btn-mini layui-btn-normal' id='diyBtn2_" +treeNode.id+ "' title='"+treeNode.name+"' onfocus='this.blur();'><i class='layui-icon'>&#xe642;</i>编辑操作权限</button><button class='layui-btn layui-btn-mini layui-btn-normal layui-btn-warm' id='diyBtn4_" +treeNode.id+ "' title='"+treeNode.name+"' onfocus='this.blur();'><i class='layui-icon'>&#xe642;</i>编辑数据权限</button><button class='layui-btn layui-btn-mini layui-btn-normal layui-btn-danger' id='diyBtn3_" +treeNode.id+ "' title='"+treeNode.name+"' onfocus='this.blur();'><i class='layui-icon'>&#xe640;</i>删除</button><button class='layui-btn layui-btn-mini' id='diyBtn5_" +treeNode.id+ "' title='"+treeNode.name+"' onfocus='this.blur();'  data-toggle='modal' data-target='.bs-example-modal-sm'>查看角色列表</button>";
            aObj.append(editStr);
            var btn = $("#diyBtn_"+treeNode.id);
            if (btn) btn.bind("click", function(){
                addNode(treeNode,'copy')
            });
            var btn2 = $("#diyBtn2_"+treeNode.id);
            if (btn2) btn2.bind("click", function(){
//                location.href = $("input[name='add']").val()
                location.href = $("input[name='action']").val()+"?id="+treeNode.id
            });
            var btn4 = $("#diyBtn4_"+treeNode.id);
            if (btn4) btn4.bind("click", function(){
//                location.href = $("input[name='add']").val()
                location.href = $("input[name='model']").val()+"?id="+treeNode.id
            });
            //查看角色列表
            var btn5 = $("#diyBtn5_"+treeNode.id);
            if (btn5) btn5.bind("click", function(){
//                location.href = $("input[name='add']").val()
                //location.href = $("input[name='model']").val()+"?id="+treeNode.id
            });
            var btn1 = $("#diyBtn1_"+treeNode.id);
            if (btn1) btn1.bind("click", function(){
                addNode(treeNode,'add')
            });
            var btn3 = $("#diyBtn3_"+treeNode.id);
            if (btn3) btn3.bind("click", function(){
                deleteNode(treeNode.id,treeNode.name)
            });
            var btn6 = $("#diyBtn6_"+treeNode.id);
            if (btn6) btn6.bind("click", function(){
                addNode(treeNode,'edit')
            });

        }
        function removeHoverDom(treeId, treeNode) {
            $("#diyBtn_"+treeNode.id).unbind().remove();
            $("#diyBtn_space_" +treeNode.id).unbind().remove();
            $("#diyBtn1_"+treeNode.id).unbind().remove();
            $("#diyBtn1_space_" +treeNode.id).unbind().remove();
            $("#diyBtn2_"+treeNode.id).unbind().remove();
            $("#diyBtn2_space_" +treeNode.id).unbind().remove();
            $("#diyBtn3_"+treeNode.id).unbind().remove();
            $("#diyBtn3_space_" +treeNode.id).unbind().remove();
            $("#diyBtn4_"+treeNode.id).unbind().remove();
            $("#diyBtn4_space_" +treeNode.id).unbind().remove();
            $("#diyBtn5_"+treeNode.id).unbind().remove();
            $("#diyBtn5_space_" +treeNode.id).unbind().remove();
            $("#diyBtn6_"+treeNode.id).unbind().remove();
            $("#diyBtn6_space_" +treeNode.id).unbind().remove();
        };
        //删除节点
        function deleteNode(id,name){
            layer.confirm('您确定要删除 '+ name + "吗?", function(){
                var url = $("input[name='delete']").val();
                var param = {"id":id,"time" : Math.random()};
                $.post(url,param,function(data){
                    if(data.success == 1){
                        layer.msg('删除成功!');
                        setTimeout(function(){ window.location.reload(); }, 3000);
                    }else{
                        layer.msg('删除失败!');
                    }
                });
            });
        }
        function addNode(e,type){
            var iId = '';
            var sName = '';
            if(type == 'add'){
                var iPId = e.id;
                var sPName  = e.name;
            }else if(type == 'copy'){
                iId = e.id;
                var iPId = e.getParentNode().id;
                var sPName = e.getParentNode().name;
            }else{
                iId = e.id;
                sName  = e.name;
                var iPId = e.getParentNode().id;
                var sPName = e.getParentNode().name;
            }
            if(typeof(name) == 'undefined'){
                sPName = '';
            }
            var modal = '<div class="modal fade" id="info-modal2" tabindex="-1" role="dialog" aria-labelledby="modal-info-label">' +
                '<div class="modal-dialog" role="document">' +
                '<div class="modal-content">' +
                '<div class="modal-header state modal-info">' +
                '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>' +
                '<h4 class="modal-title" id="modal-info-label"><i class="fa fa-info"></i></h4>' +
                '</div>' +
                '<div class="modal-body" style="min-height: 150px;">' +
                '<div class="col-md-12"><form class="form-horizontal form-stripe">' +
                '<div class="form-group">' +
                '<label for="hex-colorpicker" class="col-sm-2 control-label">角色名称:</label>' +
                '<div class="col-sm-10">' +
                '<input type="text" class="form-control" id="cn_name" data-tid="'+iId+'" value="'+sName+'">' +
                '</div>' +
                '</div>' +
                '<div class="form-group">' +
                '<label for="rgba-colorpicker" class="col-sm-2 control-label">上级角色:</label>' +
                '<div class="col-sm-10">' +
                '<input type="text" class="form-control" data-tid="'+iPId+'" id="role-picker" value="'+sPName+'" placeholder="">' +
                '</div>' +
                '</div>' +
                '</form>' +
                '</div>'+
                '</div>' +
                '<div class="modal-footer">' +
                '<button type="button" class="btn btn-info  btn-xs" id="save2" data-type="'+type+'" data-dismiss="modal">保存</button>' +
                '<button type="button" class="btn btn-default  btn-xs" id="return2" data-dismiss="modal">返回</button>' +
                '</div></div></div>' +
                '</div>';
            $("#modalbox").html("").html(modal);
            $('#info-modal2').modal({
                keyboard: false,
                backdrop:false
            });
            $('#info-modal2').on('shown.bs.modal', function (e) {
                // 关键代码，如没将modal设置为 block，则$modala_dialog.height() 为零
                $(this).css('display', 'block');
                var modalHeight=$(window).height() / 2 - $('#info-modal2 .modal-dialog').height() / 2;
                $(this).find('.modal-dialog').css({
                    'margin-top': modalHeight
                });
            });
            $("#role-picker").focus(function(){
                $('#info-modal-role').modal({
                    keyboard: false,
                    backdrop:false
                });
                $('#info-modal-role').on('shown.bs.modal', function (e) {
                    // 关键代码，如没将modal设置为 block，则$modala_dialog.height() 为零
                    $(this).css('display', 'block');
//                    var modalHeight=$(window).height() / 2 - $('#info-modal-group .modal-dialog').height() / 2;
//                    $(this).find('.modal-dialog').css({
//                        'margin-top': modalHeight
//                    });

                });

            });
            $("#save2").on("click",function(){
                $.ajax({
                    url: $("input[name='save']").val(),
                    data:{
                        data:{
                            id:$("#cn_name").attr("data-tid"),
                            cn_name:$("#cn_name").val(),
                            parent_id:$("#role-picker").attr("data-tid")
                        },
                        actions:$(this).attr('data-type')
                    },
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        if(data.success == 1){
                            layer.alert("保存成功!",function(){
                                location.reload();
                            })
                        }else{
                            layer.alert(data.errors.cn_name.message);
                        }
                    },
                    complete:function(){

                    },
                    error: function () {

                    }
                });
            })
        }
        $("#sureRole").on("click",function(e){
            console.log(e)
            var zTree = $.fn.zTree.getZTreeObj("role"),
                nodes = zTree.getSelectedNodes(),
                treeNode = nodes[0];
            console.log(treeNode);
            if (nodes.length == 0) {
                alert("请先选择一个节点");
                return;
            }
            console.log(treeNode.name)
            $("#role-picker").val(treeNode.name).attr("data-tid",treeNode.id);
        })

        function onRemove(e, treeId, treeNode) {
            console.log("onRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.id);
            $.ajax({
                url: $("input[name='del_group_info']").val(),
                data:{
                    id:treeNode.id
                },
                type: 'post',
                dataType: 'json',
                success: function (data) {
                    console.log(data)
                    if(data.success == 1){
                        alert("删除成功!")
                    }else{
                        alert(data.msg);
                    }
                },
                complete:function(){
                    location.reload();
                },
                error: function () {

                }
            });
        }
        $(document).ready(function(){
            $.fn.zTree.init($("#treeDemo"), setting, zNodes);
            $("#addParent").bind("click", {isParent:true}, addNode);
        });
        function getListData(){
            var data_list ;
            $.ajax({
                url: $("input[name='list']").val(),
                type: 'post',
                async:false,
                dataType: 'json',
                success: function (data) {
                    console.log(data)
                    if(data.success == 1){
                        data_list = data.data;
                    }else{
                    }
                },
                complete:function(){
                },
                error: function () {

                }
            });
            return data_list;
        }
        var settingGroup = {
            view: {
                selectedMulti: false
            },
            edit: {
                enable: false
            },
            data: {
                keep: {
                    parent:true,
                    leaf:true
                },
                simpleData: {
                    enable: true
                }
            }
        };
        var zNodes = getListData();
        $.each(zNodes,function(i,item){
            item.isParent = true;
        })
        $.fn.zTree.init($("#role"), settingGroup, zNodes);
        //TABLE
        $("#checkAll").click(function () {
            $("input[name='ck']").each(function () {
                if ($(this).prop("checked")) {
                    $(this).prop("checked", false);
                }
                else {
                    $(this).prop("checked", "true");
                }
            })
        });
        $(".module-content>ul>li").click(function(){
            var ind=$(this).index();
            $(this).addClass("active").siblings("li").removeClass("active");
            $(".module-content>.module-tabs>div").eq(ind).slideDown(200).siblings("div").slideUp(200);
        })

    });
</script>
<?php include_once TPL_PATH . 'public/foot_script.phtml'; ?>

<button id="top" style="height: 50px;width: 50px;"></button></body>

</html>
